<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="130px">
      <el-form-item label="退服版本开始日期" prop="versionbegin" >
        <el-date-picker clearable size="small"
          v-model="queryParams.versionbegin"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="退服版本日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="退服版本结束日期" prop="versionend" >
        <el-date-picker clearable size="small"
                        v-model="queryParams.versionend"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="退服版本日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="告警发生地市" prop="city">
        <el-select
          v-model="queryParams.city"
          placeholder="请输入告警发生地市"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        >
          <el-option
            v-for="dict in dict.type.sys_city"
            :key="dict.value"
            :label="dict.value"
            :value="dict.value"
          />
          </el-select>
      </el-form-item>


      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['tietasystem:tuifu:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['tietasystem:tuifu:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['tietasystem:tuifu:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['tietasystem:tuifu:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
          v-hasPermi="['tietasystem:tuifu:import']"
        >导入</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="tuifuList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="退服日期" align="center" prop="tuifuTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.tuifuTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="告警发生地市" align="center" prop="city" />
      <el-table-column label="网元名称" align="center" prop="neName" />
      <el-table-column label="站址编码" align="center" prop="zhanziCode" />
      <el-table-column label="是否需要发电" align="center" prop="isneedFaidtian" />
      <el-table-column label="是否发电" align="center" prop="isFadian" />
      <el-table-column label="是否与运营商确认不发电" align="center" prop="isNoFadian" />
      <el-table-column label="不发电原因" align="center" prop="noFadianReason" />
      <el-table-column label="不需要发电原因" align="center" prop="noNeedFadianReason" />
      <el-table-column label="所属运营商" align="center" prop="operator" />
      <el-table-column label="版本号" align="center" prop="version" />
<!--      <el-table-column label="修改原因时间" align="center" prop="reasonTime" width="180">-->
<!--        <template slot-scope="scope">-->
<!--          <span>{{ parseTime(scope.row.reasonTime, '{y}-{m}-{d}') }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="修改原因人" align="center" prop="reasonBy" />-->
<!--      <el-table-column label="修改原因人" align="center" prop="remark" />-->
<!--      <el-table-column label="主内容修改次数" align="center" prop="mainCurrentCount" />-->
<!--      <el-table-column label="原因修改次数" align="center" prop="curentCount" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['tietasystem:tuifu:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['tietasystem:tuifu:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改停电且退服（测试一下，确认后删除此注释）对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="版本" prop="version" v-hasPermi="['tietasystem:tuifu:add']">
          <el-date-picker clearable size="small"
                          v-model="form.version"
                          type="date"
                          value-format="yyyy-MM-dd"
                          v-hasPermi="['tietasystem:tuifu:add']"
                          placeholder="请输入版本">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="退服日期" prop="tuifuTime">
          <el-date-picker clearable size="small"
            v-model="form.tuifuTime"
            type="date"
            value-format="yyyy-MM-dd"
            :disabled="isedit"
            placeholder="选择退服日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="告警发生地市" prop="city">
          <el-select v-model="form.city" :disabled="isedit" placeholder="请选择">
            <el-option
              v-for="dict in dict.type.sys_city"
              :key="dict.value"
              :label="dict.value"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="网元名称" prop="neName">
          <el-input v-model="form.neName"  :disabled="isedit" placeholder="请输入网元名称" />
        </el-form-item>
        <el-form-item label="站址编码" prop="zhanziCode">
          <el-input v-model="form.zhanziCode" :disabled="isedit" placeholder="请输入站址编码" />
        </el-form-item>
        <el-form-item label="是否需要发电" prop="isneedFaidtian">
          <el-select v-model="form.isneedFaidtian"  placeholder="请选择">
            <el-option
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.label"
              :label="dict.label"
              :value="dict.label"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否发电" prop="isFadian">
          <el-select v-model="form.isFadian" placeholder="请选择">
            <el-option
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.label"
              :label="dict.label"
              :value="dict.label"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否与运营商确认不发电" prop="isNoFadian">
          <el-select v-model="form.isNoFadian" placeholder="请选择">
            <el-option
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.label"
              :label="dict.label"
              :value="dict.label"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="不发电原因" prop="noFadianReason">
          <el-input v-model="form.noFadianReason" placeholder="请输入不发电原因" />
        </el-form-item>
        <el-form-item label="不需要发电原因" prop="noNeedFadianReason">
          <el-input v-model="form.noNeedFadianReason" placeholder="请输入不需要发电原因" />
        </el-form-item>
        <el-form-item label="所属运营商" prop="operator">
          <el-input v-model="form.operator" placeholder="请输入所属运营商" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 用户导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <label class="font-noraml" v-hasPermi="['tietasystem:tuifu:add']">版本: </label>
      <el-date-picker
        v-model="dateRange"
        style="width: 240px"
        value-format="yyyy-MM-dd"
        type="date"
        v-hasPermi="['tietasystem:tuifu:add']"
      ></el-date-picker>
      <p></p>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?dateRange=' + dateRange"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTuifu, getTuifu, delTuifu, addTuifu, updateTuifu, exportTuifu, importTemplate } from "@/api/tietasystem/tuifu";
import { getToken } from "@/utils/auth";
import { checkPermi, checkRole } from "@/utils/permission"; // 权限判断函数
export default {
  name: "Tuifu",
  dicts: ['sys_city','sys_yes_no'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 停电且退服（测试一下，确认后删除此注释）表格数据
      tuifuList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      isedit: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tuifuTime: null,
        city: null,
        zhanziCode: null,
        isneedFaidtian: null,
        neName: null,
        isFadian: null,
        isNoFadian: null,
        noFadianReason: null,
        noNeedFadianReason: null,
        operator: null,
        reasonTime: null,
        reasonBy: null,
        mainCurrentCount: null,
        curentCount: null,
        versionbegin: null,
        versionend: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        tuifuTime: [
          { required: true, message: "退服日期不能为空", trigger: "blur" }
        ],
        city: [
          { required: true, message: "告警发生地市不能为空", trigger: "blur" }
        ],
        zhanziCode: [
          { required: true, message: "站址编码不能为空", trigger: "blur" }
        ],
        version: [
          { required: true, message: "版本不能为空", trigger: "blur" }
        ],
      },
      dateRange: '',
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        // updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/tietasystem/tuifu/importData"
      },
    };
  },
  created() {
    this.getList();
    this.isedit = checkPermi(['tietasystem:tuifu:add'])?false:true;
  },
  methods: {
    /** 查询停电且退服（测试一下，确认后删除此注释）列表 */
    getList() {
      this.loading = true;
      listTuifu(this.queryParams).then(response => {
        this.tuifuList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        tuifuTime: null,
        city: null,
        zhanziCode: null,
        isneedFaidtian: null,
        isFadian: null,
        isNoFadian: null,
        noFadianReason: null,
        noNeedFadianReason: null,
        createTime: null,
        operator: null,
        neName: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        reasonTime: null,
        reasonBy: null,
        remark: null,
        mainCurrentCount: null,
        curentCount: null,
        version: null,
        versionbegin: null,
        versionend: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加停电且退服";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTuifu(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改停电且退服";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTuifu(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTuifu(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除停电且退服（测试一下，确认后删除此注释）编号为"' + ids + '"的数据项？').then(function() {
        return delTuifu(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有停电且退服数据项？').then(() => {
        this.exportLoading = true;
        return exportTuifu(queryParams);
      }).then(response => {
        this.$download.name(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    },
    /** 导入按钮操作 */
    handleImport() {
      this.dateRange = null;
      this.upload.title = "导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      importTemplate().then(response => {
        this.$download.name(response.msg);
      });
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {

      this.$refs.upload.submit();
    }
  }
};
</script>
